package org.dea.deploy;

import static org.junit.Assert.*;
import static org.junit.Assert.fail;

import java.io.File;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ProjectDeployTest {

	private ProjectDeploy deployer;

	@Before
	public void setUp() throws Exception {
		deployer = new ProjectDeploy();
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testPrepareDir() throws Exception {
		File dir = new File("/tmp/testPrepareDir/");
		deployer.prepareDir("localhost", dir.getAbsolutePath());
		assertTrue(dir.exists());
		assertEquals(0, dir.list().length);
		assertTrue(dir.delete());
		assertTrue(!dir.exists());
		
		deployer.prepareDir("localhost", dir.getAbsolutePath());
		assertTrue(dir.exists());
		assertEquals(0, dir.list().length);
		assertTrue(dir.delete());
		assertTrue(!dir.exists());
		
		deployer.prepareDir("127.0.0.1", dir.getAbsolutePath());
		assertTrue(dir.exists());
		assertEquals(0, dir.list().length);
		assertTrue(dir.delete());
		assertTrue(!dir.exists());
	}

	@Test
	public void testTarProject() throws Exception {
		File tar = deployer.tarProject("testTarProject", ".");
		assertTrue( tar.exists() );
		assertTrue( tar.length() > 0 );
		assertTrue( tar.delete() );
		tar = deployer.tarProject("testTarProject", ".");
		assertTrue( tar.exists() );
		assertTrue( tar.length() > 0 );
		assertTrue( tar.delete() );
	}

	@Test
	public void testCopyProject() throws Exception {
		File project = new File(".");
		File dir = new File("/tmp/testCopyProject/");
		String host = "localhost";
		
		deployer.prepareDir(host, dir.getAbsolutePath());
		File tar = deployer.tarProject("testTarProject", project.getAbsolutePath() );
		deployer.copyProject(host, dir.getAbsolutePath(), tar);
		
		assertTrue(dir.exists());
		assertEquals(project.list().length+1, dir.list().length); 
		
		//again
		
		deployer.prepareDir(host, dir.getAbsolutePath());
		tar = deployer.tarProject("testTarProject", project.getAbsolutePath() );
		deployer.copyProject(host, dir.getAbsolutePath(), tar);
		
		assertTrue(dir.exists());
		assertEquals(project.list().length+1, dir.list().length); 
		
		// and again
		
		project = new File(".");
		dir = new File("/tmp/testCopyProject2/");
		host = "127.0.0.1";
		
		deployer.prepareDir(host, dir.getAbsolutePath());
		tar = deployer.tarProject("testTarProject", project.getAbsolutePath() );
		deployer.copyProject(host, dir.getAbsolutePath(), tar);
		
		assertTrue(dir.exists());
		assertEquals(project.list().length+1, dir.list().length); 
		
	}

	@Test
	public void testExecuteMain() throws Exception {
		File project = new File(".");
		File dir = new File("/tmp/testCopyProject/");
		String host = "localhost";
		int port = 9090;
		
		assertEquals( true, deployer.shutdown(host, port, "IFPing" ) );
		Thread.sleep(1000);
		assertEquals( false, deployer.ping(host, port, "IFPing" ) );
		
		deployer.prepareDir(host, dir.getAbsolutePath());
		File tar = deployer.tarProject("testTarProject", project.getAbsolutePath() );
		deployer.copyProject(host, dir.getAbsolutePath(), tar);
		deployer.executeMain(host, "./bin", dir.getAbsolutePath(), "RMIPing", "", port);
		
		Thread.sleep(1000);
		assertEquals( true, deployer.ping(host, port, "IFPing" ) );
		assertEquals( true, deployer.shutdown(host, port, "IFPing" ) );
		
		Thread.sleep(2000);
		assertEquals( false, deployer.ping(host, port, "IFPing" ) );
		
		deployer.prepareDir(host, dir.getAbsolutePath());
		tar = deployer.tarProject("testTarProject", project.getAbsolutePath() );
		deployer.copyProject(host, dir.getAbsolutePath(), tar);
		deployer.executeMain(host, "./bin", dir.getAbsolutePath(), "RMIPing", "", port);
		
		Thread.sleep(1000);
		assertEquals( true, deployer.ping(host, port, "IFPing" ) );
		assertEquals( true, deployer.shutdown(host, port, "IFPing" ) );
		
		Thread.sleep(2000);
		assertEquals( false, deployer.ping(host, port, "IFPing" ) );		
		
	}

}
